#!/bin/bash

for i in $(seq 1 5); do
killall systemd-journald
sleep 0.5
done

rm -f /etc/fstab
rm -f /etc/mtab
rm -f /etc/apt/sources.list~
rm -Rf /etc/systemd/system/timers.target.wants
rm -f /etc/systemd/system/multi-user.target.wants/ssh.service
rm -f /etc/systemd/system/multi-user.target.wants/dnsmasq.service

rm -f /etc/ssh/ssh_host*

rm -f /var/backups/*
rm -f /var/cache/ldconfig/*
rm -f /var/cache/debconf/*
rm -Rf /var/cache/apparmor/*
rm -f /var/cache/fontconfig/*
rm -Rf /var/cache/man/*
rm -f /var/lib/apt/extended_states
rm -f /var/lib/systemd/random-seed
rm -f /var/lib/apt/lists/deb.*
rm -Rf /root/.local/share/mc
rm -Rf /root/.cache
rm -f /root/.wget-hsts

rm -f /var/lib/dpkg/*-old
rm -f /var/log/*
rm -f /var/log/*/*
rm -f /var/log/*/*/*
rm -Rf /var/log/journal/*
rm -f /var/cache/apt/archives/*.deb
rm -Rf /var/lib/connman/*
rm -f /var/cache/apt/*.bin
rm -f /var/cache/debconf/*-old
rm -f /var/lib/dhcp/dhclient.leases
rm -f /root/.bash_history
rm -f /root/.wget-hsts
rm -Rf /usr/share/doc/*
rm -Rf /usr/share/info/*
rm -f /usr/share/images/fluxbox/debian-squared.jpg
rm -Rf /usr/share/fluxbox/nls/??*
rm -Rf /usr/share/gnome/help

rm -Rf /usr/share/locale/??
rm -Rf /usr/share/locale/??_*
rm -Rf /usr/share/locale/??@*
rm -Rf /usr/share/locale/???
rm -Rf /usr/share/i18n/locales/*_*
rm -Rf /usr/share/man/??
rm -Rf /usr/share/man/??.*
rm -Rf /usr/share/man/*_*

rm -Rf /usr/share/icons/elementaryXubuntu-dark
rm -Rf /usr/share/icons/gnome/256x256
rm -Rf /usr/share/icons/Adwaita/cursors
rm -Rf /usr/share/icons/Adwaita/512x512
rm -Rf /usr/share/icons/Adwaita/256x256
rm -Rf /usr/share/icons/Adwaita/64x64
rm -Rf /usr/share/icons/Adwaita/96x96
rm -Rf /usr/share/icons/Adwaita/scalable*
rm -Rf /usr/share/yudit/*

rm /usr/share/applications/compton.desktop
rm /usr/share/applications/debian-uxterm.desktop
rm /usr/share/applications/debian-xterm.desktop
rm /usr/share/applications/htop.desktop
rm /usr/share/applications/mc.desktop
rm /usr/share/applications/mcedit.desktop
rm /usr/share/applications/pcmanfm-desktop-pref.desktop
rm /usr/share/applications/python2.7.desktop
rm /usr/share/applications/python3.7.desktop
rm /usr/share/applications/python3.11.desktop
rm /usr/share/applications/python3.*.desktop
rm /usr/share/applications/vim.desktop
rm /usr/share/applications/SciTE.desktop
rm /usr/share/applications/org.gnome.Calculator.desktop
rm /usr/share/applications/panel*.desktop

rm /etc/systemd/system/media-sr0.mount
rm /etc/systemd/system/multi-user.target.wants/media-sr0.mount
rm /etc/systemd/system/media-sda1.mount
rm /etc/systemd/system/multi-user.target.wants/media-sda1.mount

rm /etc/.pwd.lock
rm /etc/console-setup/cached*
rm /root/.gtk-bookmarks

# newer modules are already available for Debian 11.2
rm -Rf /usr/lib/modules/5.10.0-10-amd64
rm -Rf /usr/lib/modules/5.10.0-10-686-pae
rm -Rf /usr/lib/modules/5.10.0-11-amd64
rm -Rf /usr/lib/modules/5.10.0-11-686-pae
rm -Rf /usr/lib/modules/5.10.0-13-amd64
rm -Rf /usr/lib/modules/5.10.0-13-686-pae

# Unzip gzipped files (man pages), so LZMA can compress 2times better.
# First we fix symlinks, then uncompress files
# $1 = search directory
uncompress_files()
{
   local LINK LINE

   find "$1" -type l -name "*.gz" | while read LINE; do
      LINK="$(readlink "$LINE" | sed -r 's/.gz$//')"
      FILE="$(echo "$LINE" | sed -r 's/.gz$//')"
      ln -sfn "$LINK" "$FILE"
      rm -f "$LINE"
   done
   find "$1" -type f -name "*.gz" | xargs -r gunzip -f
}

uncompress_files /etc/alternatives
uncompress_files /usr/share/man

# remove broken links
# $1 = search directory
remove_broken_links()
{
   find "$1" -type l -exec test ! -e {} \; -print | xargs rm -vf
}

remove_broken_links /etc/alternatives
remove_broken_links /usr/share/man
remove_broken_links /usr/share/scite

